<template>
  <div class="feedback-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>意见反馈管理</span>
        </div>
      </template>

      <!-- 搜索区域 -->
      <!-- <div class="search-area">
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="标题">
            <el-input v-model="searchForm.title" placeholder="请输入标题" clearable />
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="searchForm.type" placeholder="请选择类型" clearable>
              <el-option label="建议" value="SUGGESTION" />
              <el-option label="投诉" value="COMPLAINT" />
              <el-option label="咨询" value="QUESTION" />
              <el-option label="其他" value="OTHER" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
              <el-option label="待处理" value="PENDING" />
              <el-option label="处理中" value="PROCESSING" />
              <el-option label="已解决" value="RESOLVED" />
              <el-option label="已关闭" value="CLOSED" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div> -->

      <!-- 表格区域 -->
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <!-- <el-table-column prop="title" label="标题" show-overflow-tooltip /> -->
        <el-table-column prop="content" label="内容" show-overflow-tooltip />
        <el-table-column prop="type" label="类型" width="100">
          <template #default="{ row }">
            <el-tag :type="getTypeTag(row.type)">
              {{ getTypeText(row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusTag(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="提交时间" width="180" />
        <!-- <el-table-column prop="reply" label="回复内容" show-overflow-tooltip /> -->
        <!-- <el-table-column prop="replyTime" label="回复时间" width="180" /> -->
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button-group>
              <!-- <el-button 
                type="primary" 
                link 
                @click="handleReply(row)"
                v-if="row.status === 'PENDING'"
              >
                回复
              </el-button> -->
              <el-button type="primary" link @click="handleView(row)">
                查看
              </el-button>
              <el-button type="danger" link @click="handleDelete(row)">
                删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="page.current"
          v-model:page-size="page.size"
          :total="page.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 回复对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="回复反馈"
      width="500px"
    >
      <el-form ref="formRef" :model="formData" label-width="80px">
        <!-- <el-form-item label="反馈标题">
          <div>{{ formData.title }}</div>
        </el-form-item> -->
        <el-form-item label="反馈内容">
          <div>{{ formData.content }}</div>
        </el-form-item>
        <!-- <el-form-item label="回复内容" prop="reply">
          <el-input
            v-model="formData.reply"
            type="textarea"
            :rows="4"
            placeholder="请输入回复内容"
          />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitReply">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getFeedbackPage, removeFeedback, replyFeedback } from '@/api/feedback'

// 搜索表单
const searchForm = ref({
  title: '',
  type: '',
  status: ''
})

// 分页参数
const page = ref({
  current: 1,
  size: 10,
  total: 0
})

// 表格数据
const loading = ref(false)
const tableData = ref([])

// 对话框相关
const dialogVisible = ref(false)
const formData = ref({
  id: '',
  title: '',
  content: '',
  reply: ''
})

// 获取反馈列表
const fetchFeedbacks = async () => {
  loading.value = true
  try {
    const params = {
      current: page.value.current,
      size: page.value.size,
      title: searchForm.value.title,
      type: searchForm.value.type,
      status: searchForm.value.status
    }
    const res = await getFeedbackPage(params)
    tableData.value = res.records
    page.value.total = res.total
  } catch (error) {
    ElMessage.error('获取反馈列表失败')
  } finally {
    loading.value = false
  }
}

// 类型和状态的展示
const getTypeTag = (type) => {
  const types = {
    'SUGGESTION': 'success',
    'COMPLAINT': 'danger',
    'QUESTION': 'warning',
    'OTHER': 'info'
  }
  return types[type] || 'info'
}

const getTypeText = (type) => {
  const texts = {
    'SUGGESTION': '建议',
    'COMPLAINT': '投诉',
    'QUESTION': '咨询',
    'OTHER': '其他'
  }
  return texts[type] || type
}

const getStatusTag = (status) => {
  const types = {
    'PENDING': 'warning',
    'PROCESSING': 'primary',
    'RESOLVED': 'success',
    'CLOSED': 'info'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    'PENDING': '待处理',
    'PROCESSING': '处理中',
    'RESOLVED': '已解决',
    'CLOSED': '已关闭'
  }
  return texts[status] || status
}

// 搜索
const handleSearch = () => {
  page.value.current = 1
  fetchFeedbacks()
}

// 重置搜索
const handleReset = () => {
  searchForm.value = {
    title: '',
    type: '',
    status: ''
  }
  handleSearch()
}

// 回复反馈
const handleReply = (row) => {
  formData.value = { ...row }
  dialogVisible.value = true
}

// 提交回复
const submitReply = async () => {
  if (!formData.value.reply) {
    ElMessage.warning('请输入回复内容')
    return
  }

  try {
    await replyFeedback(
      formData.value.id,
      formData.value.reply,
      JSON.parse(localStorage.getItem('userInfo')).id
    )
    ElMessage.success('回复成功')
    dialogVisible.value = false
    fetchFeedbacks()
  } catch (error) {
    ElMessage.error('回复失败')
  }
}

// 查看详情
const handleView = (row) => {
  formData.value = { ...row }
  dialogVisible.value = true
}

// 删除反馈
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除这条反馈吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    try {
      await removeFeedback(row.id)
      ElMessage.success('删除成功')
      fetchFeedbacks()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

// 分页大小改变
const handleSizeChange = (val) => {
  page.value.size = val
  fetchFeedbacks()
}

// 当前页改变
const handleCurrentChange = (val) => {
  page.value.current = val
  fetchFeedbacks()
}

// 初始化
fetchFeedbacks()
</script>

<style scoped>
.feedback-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-area {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 